package cn.suimg.shortlink.controller;

import cn.suimg.shortlink.bean.ShortLink;
import cn.suimg.shortlink.exception.ServiceException;
import cn.suimg.shortlink.request.BasicController;
import cn.suimg.shortlink.response.ServerResponse;
import cn.suimg.shortlink.service.ShortLinkService;
import cn.suimg.shortlink.util.DateUtil;
import cn.suimg.shortlink.vo.User;
import cn.suimg.web.util.PrincipalUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.*;

@RestController
@RequestMapping("api")
public class ApiController extends BasicController {

    @Resource
    private ShortLinkService shortLinkService;

    /**
     * 过期时间枚举列表(1天,1星期,1个月)
     */
    private static final List<Integer> expireSecondList = Arrays.asList(86400,604800,2592000);

    /**
     * 获取我创建的短链接列表
     * @param token
     * @param principal
     * @return
     */
    @GetMapping("list.do")
    public ServerResponse getShortLinkList(String token, Principal principal){
        User currentUser = getCurrentUser(token, principal);
        if(currentUser == null || currentUser.getId() == null)
            return ServerResponse.failed("please login");
        List<ShortLink> shortLinkList = shortLinkService.queryShortLinkList(currentUser.getId());
        return ServerResponse.success("query success",shortLinkList);
    }

    /**
     * 创建短链接
     * @param token
     * @param principal
     * @param originalLink
     * @param expireSecond
     * @return
     */
    @PostMapping("create.do")
    public ServerResponse createShortLink(String token, Principal principal, String originalLink, Integer expireSecond){
        User currentUser = getCurrentUser(token, principal);
        if(currentUser == null || currentUser.getId() == null)
            return ServerResponse.failed("please login");
        if(expireSecondList.contains(expireSecond)){
            try{
                String shortLink = shortLinkService.createShortLink(originalLink, currentUser.getId(), expireSecond);
                Date expireDate = new Date(System.currentTimeMillis() + expireSecond * 1000);
                return ServerResponse.success("create success",new HashMap<String,Object>(){{
                    put("shortLink","https://smg.pub/" + shortLink);
                    put("expireTime",expireDate);
                    put("expireTimeFormat", DateUtil.parseDateTime(expireDate));
                }});
            }catch (ServiceException e){
                return ServerResponse.failed(e.getMessage());
            }
        }
        return ServerResponse.failed("expire second must in:[86400,604800,2592000]");
    }


    @GetMapping("disableOrEnable.do")
    public ServerResponse disableOrEnable(String token, Principal principal,Integer id){
        User currentUser = getCurrentUser(token, principal);
        if(currentUser == null || currentUser.getId() == null)
            return ServerResponse.failed("please login");
        try{
            boolean result = shortLinkService.enableOrDisableLink(currentUser.getId(), id);
            return ServerResponse.success(String.format("%s success",result ? "enable" : "disable"));
        }catch (ServiceException e){
            return ServerResponse.failed(e.getMessage());
        }
    }



    public User getCurrentUser(String token, Principal principal){
        return principal != null ? PrincipalUtil.getDetails(principal) : PrincipalUtil.getDetails(token);
    }



}
